package com.nbicc.catalog.service.impl;

import com.nbicc.catalog.mapper.CatMapper;
import com.nbicc.catalog.myenum.CatTypeEnum;
import com.nbicc.catalog.service.ICatalogService;
import com.nbicc.catalog.po.Cat;
import com.nbicc.common.TreeNode;
import com.nbicc.common.utils.TreeBuilder;
import com.nbicc.newscat.po.NewsCat;
import com.nbicc.newscat.service.INewsCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("CatalogServiceImpl")
public class CatalogServiceImpl implements ICatalogService{

    @Autowired
    private CatMapper catMapper;
    @Resource(name = "NewsCatServiceImpl")
    private INewsCatService newsCatService;

    @Override
    public Integer insert(Cat cat) throws Exception{
        if(!isAllow(cat)){
            throw  new Exception("不允许建此节点");
        }
        cat.setAllowChild(true);
        cat.setType(CatTypeEnum.cat);
        return catMapper.insert(cat);
    }

    @Override
    public Cat selectById(Number id) {
        return catMapper.selectById(id);
    }

    @Override
    public Integer updateById(Object c) throws Exception{
        return catMapper.updateById(c);
    }

    @Transactional
    @Override
    public Integer deleteById(Number id) {
        int deleteSucceeded = 0;
        List<Cat> cats = listChild(id);
        for (Cat cat : cats) {
            deleteSucceeded += catMapper.deleteById(cat.getId());
            Map<String , Object> parms = new HashMap<>();
            parms.put(NewsCat.FIELD_CAT_ID, cat.getId());
            newsCatService.deleteByMap(parms);
        }
        return deleteSucceeded;
    }

    @Override
    public List<Cat> listChild(Number id) {
        return catMapper.listChild(id);
    }

    @Override
    public List<TreeNode> listChildrenByPidInTree(int pid) {
        List<Cat> cats = catMapper.listChild(pid);
        return TreeBuilder.buildListToTree(cats , null);
    }

    @Override
    public void transLink(Cat cat) throws Exception{
        List<Cat> cats = listChild(cat.getId());
        if(cats.size()>1){
            throw new Exception("不能有子目录" , new Throwable("非法操作"));
        }
        Map<String , Object> parms = new HashMap<>();
        parms.put(NewsCat.FIELD_CAT_ID , cat.getId());
        if(newsCatService.countNews(parms) != 1){
            throw new Exception("目录下 有且只有一条新闻" , new Throwable("非法操作"));
        }
        parms.clear();
        parms.put(Cat.FIELD_ID,cat.getId());
        parms.put(Cat.FIELD_TYPE,CatTypeEnum.link);
        parms.put(Cat.FIELD_ALLOWCHILD,false);
        updateById(parms);
    }

    @Override
    public void transCat(Cat cat) throws Exception {
        Map<String , Object> parms = new HashMap<>();
        parms.put(Cat.FIELD_ID,cat.getId());
        parms.put(Cat.FIELD_TYPE,CatTypeEnum.cat);
        parms.put(Cat.FIELD_ALLOWCHILD,true);
        updateById(parms);
    }

    @Override
    public List<Cat> selectByNewsId(long newsId) {
        return catMapper.selectByNewsId(newsId);
    }

    private boolean isAllow(Cat cat){
        if(cat.getPid() == 0){
            return true;
        }
        Cat pCat = catMapper.selectById(cat.getPid());
        return pCat.isAllowChild();
    }

}
